export function deepEqual(object1, object2) {
   const keys1 = Object.keys(object1);
   const keys2 = Object.keys(object2);
   function isObject(obj) {
      return Object.prototype.toString.call(obj) === '[object Object]'
   }
   if (keys1.length !== keys2.length) {
      return false;
   }
   for (const key of keys1) {
      const val1 = object1[key];
      const val2 = object2[key];
      const areObjects = isObject(val1) && isObject(val2);
      if (
         areObjects && !deepEqual(val1, val2) || !areObjects && val1 !== val2
      ) {
         return false;
      }
   }
   return true;
};
export function getDateTime(){
   var myDate = new Date();
   var nowY = myDate.getFullYear();
   var nowM = myDate.getMonth()+1;
   var nowD = myDate.getDate();
   var enddate = nowY+"-"+(nowM<10 ? "0" + nowM : nowM)+"-"+(nowD<10 ? "0"+ nowD : nowD);//当前日期

   var lw = new Date(myDate - 1000 * 60 * 60 * 24 * 30);//最后一个数字30可改，30天的意思
   var lastY = lw.getFullYear();
   var lastM = lw.getMonth()+1;
   var lastD = lw.getDate();
   var startdate=lastY+"-"+(lastM<10 ? "0" + lastM : lastM)+"-"+(lastD<10 ? "0"+ lastD : lastD);//三十天之前日期
   return {enddate,startdate}
};

export function setTypeListSelect(str,type){
   if(type == 'number') {
      return str=='只读'?1:str=='可读可导出'?2:str=='内容编辑'?3:str=='元数据编辑'?4:str=='管理权限'?5:''
   } else {
      return str==1?'只读':str==2?'可读可导出':str==3?'内容编辑':str==4?'元数据编辑':str==5?'管理权限':''
   }
}

export function setData(numList,strList,data,that){
   var num = [],str = []
   if(Array.isArray(data)){
      for(var i=0;i<data.length;i++){
         num.push(data[i][numList])
         str.push(that.parseTime(new Date(data[i][strList]),"{m}-{d}"))
      }
   }
   return {str,num}
}

export function  filterParams(obj,arr){
   let result = {}
   for(var item in obj){
      if(arr.indexOf(item) == -1) {
         result[item]= obj[item]
      }
   }
   return result;
}
export function getHeight(isPage){
   var operArr = document.getElementsByClassName('operation');
   var searchArr = document.getElementsByClassName("search-item");
   var win =  window.innerHeight
   var search = searchArr.length?searchArr[0].clientHeight:0;
   var operation = operArr.length?operArr[0].clientHeight:0;
   var pageH = isPage ? (31+25) : 0;
   return win - search - 185 - pageH - operation;
}

/**
* 千分位分隔符
* @param {*} num 需要分割的数字
* @param {String} symbol 分隔符
* @return {String} 1,234,567
*/
export function thousandFormat(num, symbol = ',') {
   const DIGIT_PATTERN = /(^|\s)\d+(?=\.?\d*($|\s))/g
   const MILI_PATTERN = /(?=(?!\b)(\d{3})+\.?\b)/g
   return num && num.toString().replace(DIGIT_PATTERN, m => m.replace(MILI_PATTERN, symbol))
}

export function replacer(key, value) {
   if(key === 'score') {
      if (value === 100) {
         return {point: value, level: 'S'};
      } else if(value >= 90) {
         return {point: value, level: 'A'};
      } else if(value >= 70) {
         return {point: value, level: 'B'};
      } else if(value >= 50) {
         return {point: value, level: 'C'};
      } else {
         return {point: value, level: 'E'};
      }
   }
   return value;
}

export function guid() {
   return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
       // eslint-disable-next-line no-bitwise
       const r = Math.random() * 16 | 0,
           // eslint-disable-next-line no-bitwise
           v = c === 'x' ? r : (r & 0x3 | 0x8)
       return v.toString(16)
   })
}

/**
 * val值不为空字符，null，undefined
 */
export const isNotEmpty = val => {
   const arr = [undefined, null, ''];
   return !arr.includes(val);
};

const _toString = Object.prototype.toString;

const typeChecker = [
  'Null',
  'Undefined',
  'Boolean',
  'String',
  'Number',
  'Object',
  'Function',
  'Array',
  'Error'
].reduce((prev, type) => {
  return {
    ...prev,
    [type]: val => _toString.call(val) === `[object ${type}]`
  };
}, {});

export const isNull = typeChecker['Null'];
export const isUndefined = typeChecker['Undefined'];
export const isBoolean = typeChecker['Boolean'];
export const isString = typeChecker['String'];
export const isNumber = typeChecker['Number'];
export const isObject = typeChecker['Object'];
export const isFunction = typeChecker['Function'];
export const isArray = typeChecker['Array'];
export const isError = typeChecker['Error'];